package com.wits.qso.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.wits.comet.chat.common.CometCommand;
import com.wits.comet.chat.common.CometDomain;
import com.wits.comet.chat.service.ChatRoomService;
import com.wits.platform.common.utils.DateUtil;
import com.wits.platform.service.account.ShiroUser;
import com.wits.qso.common.enumerate.MemberStatus;
import com.wits.qso.dao.DiscussItemsDao;
import com.wits.qso.dao.DiscussionMemberDao;
import com.wits.qso.entity.DiscussItems;

@Component
public class InstructService {

	@Resource
	private DiscussItemsDao discussItemsDao;
	
	@Resource
	private ChatRoomService chatRoom;
	
	@Resource
	private DiscussionMemberDao memberDao;
	
	/**
	 * 
	 * 发送消息
	 * @param groupId
	 * @param sender
	 * @param message
	 * @throws IOException
	 */
	public void sendToGroup(String groupId,ShiroUser sender,String command,String message) throws IOException{
		List<String> memberIdList=memberDao.findMemberIdByGroupIdAndNotEqStatus(groupId,MemberStatus.deleted.value());
		if(memberIdList!=null && memberIdList.size()>0){
			DiscussItems item=new DiscussItems(groupId,sender.getId(),sender.getName(),message);
			item.setSendDate(DateUtil.getDateString());
			item.setSendTime(DateUtil.getTimeString());
			item.setDomain(CometDomain.group.value());
			item.setCommand(command);
			//如果不是为了保持连接的系统消息,则进行持久化
			if(!CometCommand.keepCall.value().equals(command)){
				discussItemsDao.save(item);
			}
			chatRoom.broadcast(memberIdList, item);
		}

	}
	
	
	/** 
	 * 
	 * 发送通知(老师开始指导时,通知学生)
	 */
	public void sendNotify(CometDomain domain,CometCommand command,String groupId,ShiroUser sender,Collection<String> memberList){
		sendNotify(domain,command,groupId,sender,memberList,groupId);
	}
	/** 
	 * 
	 * 发送通知(老师开始指导时,通知学生)
	 */
	public void sendNotify(CometDomain domain,CometCommand command,String groupId,ShiroUser sender,Collection<String> memberList,Object data){
		DiscussItems item=new DiscussItems(groupId,sender.getId(),sender.getName());
		item.setDomain(domain.value());//CometDomain.main.value()
		item.setCommand(command.value());//CometCommand.instruct.value()
		item.setData(data);
		chatRoom.broadcast(memberList,sender.getId(),item);
	}
	
	
	/** 
	 * 
	 * 发送通知(老师开始指导时,通知学生)
	 */
	public void sendNotify(CometDomain domain,CometCommand command,ShiroUser sender,Collection<String> memberList){
		DiscussItems item=new DiscussItems();
		item.setSendUserId(sender.getId());
		item.setSendUserName(sender.getName());
		item.setDomain(domain.value());
		item.setCommand(command.value());
		chatRoom.broadcast(memberList,sender.getId(),item);
	}
	
	
	/** 
	 * 
	 * 发送通知(老师开始指导时,通知学生)
	 */
	public void sendNotify(CometDomain domain,CometCommand command,Collection<String> memberList){
		DiscussItems item=new DiscussItems();
		item.setDomain(domain.value());
		item.setCommand(command.value());
		chatRoom.broadcast(memberList,item);
	}
	
	/** 
	 * 老师上课或者下课，发送消息通知学生
	 * @param domain
	 * @param command
	 * @param memberList
	 * @param message
	 * @param data
	 */
	public void teachMsg(CometDomain domain,CometCommand command,Collection<String> memberList,String message,Object data){
		DiscussItems item=new DiscussItems();
		item.setDomain(domain.value());
		item.setCommand(command.value());
		item.setContext(message);
		item.setData(data);
		chatRoom.broadcast(memberList,item);
	}
	
	
	public void sendNotify(CometDomain domain,CometCommand command,String memberId){
		List<String> list=new ArrayList<String>();
		list.add(memberId);
		sendNotify(domain,command,list);
	}
}
